package com.sangeng.handler.exception;

import com.sangeng.domain.ResponseResult;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @auther qihs
 * @Date
 * 统一异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //异常处理器，绑定处理的异常
    @ExceptionHandler(SystemException.class)
    public ResponseResult systemExceptionHandler(SystemException e){
        //打印异常信息

        log.error("出现了异常 ！ {}", e);
        //从异常信息中获取提示信息，封装成ResponseResult对象返回
        //ResponseResult result = ResponseResult.errorResult(e.getCode(), e.getMsg());
        return ResponseResult.errorResult(e.getCode(), e.getMsg());
    }


    @ExceptionHandler(Exception.class)
    public ResponseResult ExceptionHandler(Exception e){
        //打印异常信息

        log.error("出现了异常 ！ {}", e);
        //从异常信息中获取提示信息，封装成ResponseResult对象返回
        //ResponseResult result = ResponseResult.errorResult(e.getCode(), e.getMsg());
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }

}
